//
//  MenuContainerController.m
//  SlidingMenu
//
//  Created by wei1 on 2016/12/4.
//  Copyright © 2016年 qge. All rights reserved.
//

#import "MenuContainerController.h"
#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height
#define Margin  50.0 //边距
#define locationCriticalValue ScreenW/2 //滑动临界值
#define speedCriticalValue 400.0


typedef enum : NSUInteger {
    PanningDirectionLeft,
    PanningDirectionRight,
} PanningDirection;

typedef enum : NSUInteger {
    CenterViewStateRight,
    CenterViewStateLeft,
    CenterViewStateMid,
} CenterViewState;

@interface MenuContainerController ()

@property(nonatomic)UIViewController* centerVC;
@property(nonatomic)UIViewController* leftVC;
@property(nonatomic)UIViewController* rightVC;
@property(nonatomic)UIView* clearCoverView;
@property(nonatomic)CGPoint centerP;
@property(nonatomic)CGPoint centerLeftP;
@property(nonatomic)CGPoint centerRightP;

@property(nonatomic)CenterViewState state;
@property(nonatomic)PanningDirection panD;

@property(nonatomic)UIPanGestureRecognizer *pan;
@property(nonatomic)UITapGestureRecognizer *tap;

@end

@implementation MenuContainerController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
}
#pragma 初始化方法
-(instancetype)initWithCenterController:(UIViewController *)centerVC leftController:(UIViewController *)leftVC rightController:(UIViewController *)rightVC{
    self.rightVC = rightVC;
    self.leftVC = leftVC;
    self.centerVC = centerVC;
    [self addChildViewController:rightVC];
    [self addChildViewController:leftVC];
    [self addChildViewController:centerVC];
    self.centerP = centerVC.view.center;
    self.centerLeftP = CGPointMake(-ScreenW/2+Margin, ScreenH/2);
    self.centerRightP = CGPointMake(ScreenW*3/2-Margin, ScreenH/2);
    [self.view addSubview:centerVC.view];
    self.state = CenterViewStateMid;
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panning:)];

    [self.centerVC.view addGestureRecognizer:pan];
    return self;
}
#pragma 懒加载

-(UIView *)clearCoverView{
    if (_clearCoverView == nil){
        _clearCoverView = [[UIView alloc]init];
        _clearCoverView.backgroundColor = [UIColor clearColor];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapping:)];
        [_clearCoverView addGestureRecognizer:tap];
    }
    return _clearCoverView;
}

-(void)setState:(CenterViewState)state{
    if (_state == state){
        return;
    }
    _state = state;
    switch (state) {
        case CenterViewStateMid:{
            if ([self.view.subviews containsObject:self.clearCoverView]){
                [self.clearCoverView removeFromSuperview];
            }

            break;
        }
        case CenterViewStateLeft:{
            self.clearCoverView.frame = CGRectMake(0, 0, Margin, ScreenH);
            [self.view addSubview:self.clearCoverView];
            break;
        }
        case CenterViewStateRight:{
            self.clearCoverView.frame = CGRectMake(ScreenW - Margin,0 , Margin, ScreenH);
            
            [self.view addSubview:self.clearCoverView];
            break;
        }
    }
}

-(void)setPanD:(PanningDirection)panD{
    if (_panD == panD){
        return;
    }
    _panD = panD;
    switch (panD) {
        case PanningDirectionLeft:{
            if (![self.view.subviews containsObject:self.rightVC.view]){
                self.rightVC.view.frame = CGRectMake(Margin, 0, ScreenW-Margin, ScreenH);
                [self.view insertSubview:self.rightVC.view belowSubview:self.centerVC.view];
            }
            if([self.view.subviews containsObject:self.leftVC.view]){
                [self.leftVC.view removeFromSuperview];
            }
            break;
        }
        case PanningDirectionRight:{
            if (![self.view.subviews containsObject:self.leftVC.view]){
                self.leftVC.view.frame = CGRectMake(0, 0, ScreenW-Margin, ScreenH);
                [self.view insertSubview:self.leftVC.view belowSubview:self.centerVC.view];
            }
            
            if([self.view.subviews containsObject:self.rightVC.view]){
                [self.rightVC.view removeFromSuperview];
            }
            break;
        }
    }
}

#pragma 拖拽
-(void)panning:(UIPanGestureRecognizer *)pan{
    CGFloat xOffset = [pan translationInView:self.view].x;
    switch (pan.state) {
        case UIGestureRecognizerStateChanged:{
            self.centerVC.view.center = CGPointMake(self.centerP.x+xOffset, self.centerP.y);
            if (xOffset>0){
                //右滑显示左窗口
                self.panD = PanningDirectionRight;
                
            }else if(xOffset<0){
                //左滑显示右窗口
                self.panD = PanningDirectionLeft;
                
            }
            break;
        }
        case UIGestureRecognizerStateEnded:{
            CGFloat speed = [pan velocityInView:self.centerVC.view].x;
            if (self.centerVC.view.center.x<self.centerLeftP.x + locationCriticalValue || speed < -speedCriticalValue){
                [self centerViewMoveToPoint:self.centerLeftP];
                self.state = CenterViewStateLeft;
            }else if(self.centerVC.view.center.x>self.centerRightP.x - locationCriticalValue || speed > speedCriticalValue){
                [self centerViewMoveToPoint:self.centerRightP];
                self.state = CenterViewStateRight;
            }else{
               [self centerViewMoveToPoint:self.centerP];
            }
            break;
        }
            
        default:
            break;
    }
}
#pragma 轻触
-(void)tapping:(UITapGestureRecognizer *)tap{
    [self centerViewMoveToPoint:self.centerP];
    self.state = CenterViewStateMid;
}

#pragma mark 移动centerView
-(void)centerViewMoveToPoint:(CGPoint)point{
    [UIView animateWithDuration:0.3 animations:^{
        self.centerVC.view.center = point;
    }];
}


@end
